Skip to content

Conversation

@han-ol
Copy link
Collaborator

@han-ol han-ol commented Mar 27, 2025

This PR seeks to adress the stability of estimating mean and covariance using

inference_network=bf.networks.PointInferenceNetwork(
        scores=dict(
            mvn=bf.scores.MultivariateNormalScore()
        )
)
# -> results in warning: MultivariateNormalScore is unstable.

It is fixed by using link function called PositiveDefinite, that is based on the Cholesky decomposition.

@han-ol han-ol changed the base branch from main to build-deepset-without-call March 27, 2025 15:38
@paul-buerkner
Copy link
Contributor

Thanks! Is it intentional that this is to be merged into the build-deepset-without-call branch instead of into main (or dev)?

@han-ol han-ol changed the base branch from build-deepset-without-call to dev March 27, 2025 15:45
@han-ol
Copy link
Collaborator Author

han-ol commented Mar 27, 2025

Thanks for pointing that out, it was not intentional. It was actually developed against the current dev branch, so I changed it accordingly.

  • issue with TensorFlow backend: the new bf.links.PositiveDefinite() relies on bf.utils.fill_triangular_matrix(). For some reason the keras.ops.tril call fails for tensorflow backend claiming "pred must not be a Python bool", without it being clear where such a pred variable is defined.
  • issue with PyTorch backend: tests for positive definiteness are failing.

I will try to reproduce the error outside of BayesFlow.

@han-ol han-ol closed this Mar 27, 2025
@han-ol han-ol reopened this Mar 27, 2025
@han-ol
Copy link
Collaborator Author

han-ol commented Mar 28, 2025

Pair programming with Valentin was successful (thx!) and we smoothed out the flaky tests. All backends support estimation of multivariate normal parameters now.

I removed some rough edges, added tests, so hopefully the codecov/path tests pass too now.

The PR is ready for review as soon as tests passed.

EDIT: tests passed, ready for review.

@han-ol han-ol self-assigned this Mar 28, 2025
self.subnets_kwargs = subnets_kwargs or {}
self.links = links or {}

self.not_transforming_like_vector = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment documenting what this should contain, so people who want to set this in a subclass know what to use

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I would also like to know if we can handle this in a better way than a special attribute.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want this to be variable within a class, i.e., does this have to be an instance variable? If not, a constant class variable might be an option, like

class ScoringRule:
    #: This variable lists ... (the #: syntax should allow sphinx to parse this)
    NOT_TRANSFORMING_LIKE_VECTOR = tuple()  # use immutable type tuple instead of list

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to use a class variable for this! Implemented it in d87b0b9.

I added documenting comments as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding a better way of handling this in general:

There is a discussion in #304 about the long term plans for special estimators and how they play with adapter transformations.
In my opinion, what we have here suffices for now and is a reasonable first step.

Copy link
Contributor

@LarsKue LarsKue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code overall looks good. See individual comments. I don't know enough about the requirements of this task to give a definite approval, but I trust that @han-ol did their research on this 🙂

Please do not merge before the individual comments are resolved, though.

def _prepare_conditions(self, conditions: dict[str, np.ndarray], **kwargs) -> dict[str, Tensor]:
"""Adapts and converts the conditions to tensors."""
conditions = self.adapter(conditions, strict=False, stage="inference", **kwargs)
conditions.pop("inference_variables", None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add this function to the ContinuousApproximator, if it is identical between it and the Point Approximator

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! This and similar refactoring of the ContinuousApproximator is a good idea (but I would keep them out of this PR).
There is also the option of moving the conversion to tensor into the adapter. Possibly with an optional bool flag convert_to_tensor that is by default False.

self.subnets_kwargs = subnets_kwargs or {}
self.links = links or {}

self.not_transforming_like_vector = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, I would also like to know if we can handle this in a better way than a special attribute.

@han-ol
Copy link
Collaborator Author

han-ol commented Apr 1, 2025

Thank you for your helpful reviews!

I resolved the conversations concerning things that are unequivocally solved. What is left open is for you to check if you are happy with the state of things.

From my side, the PR is ready to merge.

@stefanradev93 stefanradev93 merged commit f21e6ef into bayesflow-org:dev Apr 2, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants